var cost = {};

cost.store = null;
cost.createStore = function(){
    var store = new Ext.data.JsonStore({
        autoLoad: true,
        fields: ["id", "title", "value", "type", "created_date"],
        proxy: {
            type: 'rest',
            url: '/cost/getList',
            reader: {
                type: 'json',
                root: 'costs'
            }
        }
    });
    
    store.on('beforeload', function(){
        
    });
    
    store.on('load', function(){        
        
    });
    
    return store;
};

cost.createGrid = function(grid_id){
    cost.store = cost.createStore();
    
    var rowEditing = Ext.create('Ext.grid.plugin.RowEditing', {
        clicksToMoveEditor: 1,
        autoCancel: false,
        listeners : {
            edit : function(editor, o)
            {
                var n = o.newValues;
                var data = {
                  id : n.id,
                  title : n.title,
                  type : n.type,
                  value : n.value
                };
                
                $.ajax({
                    url : '/cost/update',
                    type : 'post',
                    data : data,
                    dataType : 'text',
                    success : function(data)
                    {
                        if(data == 'true')
                            cost.store.reload();
                    }
                });
            }
        }
    });
    
    var paging = new Ext.PagingToolbar( {
        store : cost.store,
        displayInfo : true,
        emptyMsg: "No items to display"
    });
    
    var grid = new Ext.grid.GridPanel({
        store: cost.store,
        id: "costList",
        columnLines: true,
        tbar: [{
            text: 'Add',
            iconCls: 'icon-add',
            handler : function() {
                rowEditing.cancelEdit();
                var r = {type:"1", id: ""};

                cost.store.insert(0, r);
                rowEditing.startEdit(0, 0);
            }
        },{
            iconCls: 'icon-delete',
            text: 'Delete',
            disabled: true,
            itemId: 'delete',
            handler: function() {
                var sm = grid.getSelectionModel();
                rowEditing.cancelEdit();
                
                var selection = sm.getSelection()[0];
                cost.deleteRow(selection.data);
                
                /*
                cost.store.remove(sm.getSelection());
                if (cost.store.getCount() > 0) {
                    sm.select(0);
                }*/
            }
        }],
        bbar: paging,
        columns: [new Ext.grid.RowNumberer(), 
        {
            text: 'ID',
            dataIndex: 'id',
            hidden: true
        },{
            text: 'Title',
            dataIndex: 'title',
            sortable: false,
            align: 'left',            
            menuDisabled: true,
            width: 336,
            editor: 'textfield'
        },{
            text: 'Value',
            dataIndex: 'value',
            sortable: false,
            align: 'right',            
            menuDisabled: true,
            xtype: 'numbercolumn',
            format: '0,000',
            editor: 'numberfield'           
        },{
            text: 'Type',
            dataIndex: 'type',
            sortable: false,
            align: 'center',            
            menuDisabled: true,
            format: '0,000',
            editor: cost.getComboType(),
            renderer: function(value)
            {
                if(value == 1) return "Widthraw";
                else if(value == 2) return "Enroll";
            }
        },{
            text: 'Created date',
            dataIndex: 'created_date',
            sortable: false,
            align: 'right',       
            width: 120,
            menuDisabled: true
        }],
        plugins: [rowEditing],
        stripeRows: true,
        width: 681,
        height: 640,
        listeners: {
            selectionchange : function(model, selections)
            {
                this.down('#delete').setDisabled(selections.length === 0);
            }
        }
    });

    return grid.render(grid_id);
};

cost.getComboType = function()
{
    var store = Ext.create('Ext.data.Store', {
        fields: ['value', 'text'],
        data : [
            {"value":"1", "text":"Widthraw"},
            {"value":"2", "text":"Enroll"}
        ]
    });
    
    var combo = new Ext.form.ComboBox({
        store: store,
        displayField: 'text',
        valueField: 'value'
    });
    
    return combo;
}

cost.deleteRow = function(item)
{
    $.ajax({
        url : '/cost/delete',
        type : 'post',
        data : {id: item.id},
        dataType : 'text',
        success : function(data)
        {
            if(data == 'true')
                cost.store.reload();
        }
    });
}

Ext.onReady(function() {
    cost.createGrid('costList');
});
